﻿Imports System.Text.RegularExpressions
Imports System.Workflow.Runtime
Imports Co9320.NicoLiveService
Imports System.Threading

Class MainWindow
    Private Context As New MainWndowViewModel
    Public Sub New()

        ' この呼び出しはデザイナーで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後で初期化を追加します。
        Me.DataContext = Context

        System.Net.ServicePointManager.DefaultConnectionLimit = 2
    End Sub

    Private Function GetVideoIds(ByVal text As String) As String()
        Dim matchs = Regex.Matches(text,
                                    "\b(?<id>(sm|nm)\d+)\b", RegexOptions.ECMAScript)
        Dim list = New List(Of String)
        For Each m As Match In matchs
            Dim id = m.Groups("id").Value
            If list.Contains(id) Then
                Continue For
            End If
            list.Add(id)
        Next
        Return list.ToArray

    End Function

    Private Sub AddVideos(ByVal html As String)
        For Each id In GetVideoIds(html)
            Dim videoId = id
            Dim vs = From v In Context.Videos
                     Where v.Id = videoId

            If vs.Count > 0 Then
                Continue For
            End If

            Dim video = New Video With {.Id = id}
            Dim viewModel = New VideoViewModel(video)
            viewModel.DownloadInfoAsync()
            Context.Videos.Add(viewModel)
        Next
    End Sub

    Private WithEvents CommentServer As New CommentServer
    Private Instance As WorkflowInstance
    Private WaitHandle As New AutoResetEvent(False)


    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim thread = New System.Threading.Thread(AddressOf StartWorkflow)
        thread.Start()
    End Sub

    Private Sub StartWorkflow()

        CommentServer.ConnectAsync("lv6749454", CookieSource.Firefox)

        Using workflowRuntime As New WorkflowRuntime()
            AddHandler workflowRuntime.WorkflowCompleted, AddressOf OnWorkflowCompleted
            AddHandler workflowRuntime.WorkflowTerminated, AddressOf OnWorkflowTerminated

            Dim videos = New List(Of Video)
            For Each v In Context.Videos
                videos.Add(v.ToModel)
            Next

            Dim dic = New Dictionary(Of String, Object) From
                {{"Videos", videos.ToArray},
                 {"CommentServer", CommentServer}}

            Dim workflowInstance As WorkflowInstance

            workflowInstance = workflowRuntime.CreateWorkflow(GetType(MainWorkflow), dic)
            Instance = workflowInstance
            workflowInstance.Start()

            WaitHandle.WaitOne()

        End Using


    End Sub

    Private Sub OnWorkflowCompleted(ByVal sender As Object, ByVal e As WorkflowCompletedEventArgs)
        WaitHandle.Set()
    End Sub
    Private Sub OnWorkflowTerminated(ByVal sender As Object, ByVal e As WorkflowTerminatedEventArgs)
        WaitHandle.Set()
    End Sub



    Private Sub MainWindow_Drop(ByVal sender As Object, ByVal e As System.Windows.DragEventArgs) Handles Me.Drop
        Dim html = e.Data.GetData(DataFormats.Html)
        If html IsNot Nothing Then
            AddVideos(CStr(html))
        End If
    End Sub
End Class
